package net.zoneland.knowledge.dao;

import java.util.List;
import net.zoneland.knowledge.model.Resource;
import net.zoneland.knowledge.model.ResourceExample;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.UpdateProvider;
import org.apache.ibatis.type.JdbcType;

@Mapper
public interface ResourceMapper {
    @SelectProvider(type=ResourceSqlProvider.class, method="countByExample")
    long countByExample(ResourceExample example);

    @Delete({
        "delete from AITK_RESOURCE_INFO",
        "where ID = #{id,jdbcType=VARCHAR}"
    })
    int deleteByPrimaryKey(String id);

    @Insert({
        "insert into AITK_RESOURCE_INFO (ID, NAME, ",
        "PID, RESOURCETYPE, ",
        "ORDERS, SCRIPTURLS, ",
        "DESCRIPTION, ICON)",
        "values (#{id,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR}, ",
        "#{pid,jdbcType=VARCHAR}, #{resourceType,jdbcType=VARCHAR}, ",
        "#{orders,jdbcType=DECIMAL}, #{scriptUrls,jdbcType=VARCHAR}, ",
        "#{description,jdbcType=VARCHAR}, #{icon,jdbcType=VARCHAR})"
    })
    int insert(Resource record);

    @InsertProvider(type=ResourceSqlProvider.class, method="insertSelective")
    int insertSelective(Resource record);

    @SelectProvider(type=ResourceSqlProvider.class, method="selectByExample")
    @Results({
        @Result(column="ID", property="id", jdbcType=JdbcType.VARCHAR, id=true),
        @Result(column="NAME", property="name", jdbcType=JdbcType.VARCHAR),
        @Result(column="PID", property="pid", jdbcType=JdbcType.VARCHAR),
        @Result(column="RESOURCETYPE", property="resourceType", jdbcType=JdbcType.VARCHAR),
        @Result(column="ORDERS", property="orders", jdbcType=JdbcType.DECIMAL),
        @Result(column="SCRIPTURLS", property="scriptUrls", jdbcType=JdbcType.VARCHAR),
        @Result(column="DESCRIPTION", property="description", jdbcType=JdbcType.VARCHAR),
        @Result(column="ICON", property="icon", jdbcType=JdbcType.VARCHAR)
    })
    List<Resource> selectByExample(ResourceExample example);

    @Select({
        "select",
        "ID, NAME, PID, RESOURCETYPE, ORDERS, SCRIPTURLS, DESCRIPTION, ICON",
        "from AITK_RESOURCE_INFO",
        "where ID = #{id,jdbcType=VARCHAR}"
    })
    @Results({
        @Result(column="ID", property="id", jdbcType=JdbcType.VARCHAR, id=true),
        @Result(column="NAME", property="name", jdbcType=JdbcType.VARCHAR),
        @Result(column="PID", property="pid", jdbcType=JdbcType.VARCHAR),
        @Result(column="RESOURCETYPE", property="resourceType", jdbcType=JdbcType.VARCHAR),
        @Result(column="ORDERS", property="orders", jdbcType=JdbcType.DECIMAL),
        @Result(column="SCRIPTURLS", property="scriptUrls", jdbcType=JdbcType.VARCHAR),
        @Result(column="DESCRIPTION", property="description", jdbcType=JdbcType.VARCHAR),
        @Result(column="ICON", property="icon", jdbcType=JdbcType.VARCHAR)
    })
    Resource selectByPrimaryKey(String id);

    @UpdateProvider(type=ResourceSqlProvider.class, method="updateByExampleSelective")
    int updateByExampleSelective(@Param("record") Resource record, @Param("example") ResourceExample example);

    @UpdateProvider(type=ResourceSqlProvider.class, method="updateByExample")
    int updateByExample(@Param("record") Resource record, @Param("example") ResourceExample example);

    @UpdateProvider(type=ResourceSqlProvider.class, method="updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(Resource record);

    @Update({
        "update AITK_RESOURCE_INFO",
        "set NAME = #{name,jdbcType=VARCHAR},",
          "PID = #{pid,jdbcType=VARCHAR},",
          "RESOURCETYPE = #{resourceType,jdbcType=VARCHAR},",
          "ORDERS = #{orders,jdbcType=DECIMAL},",
          "SCRIPTURLS = #{scriptUrls,jdbcType=VARCHAR},",
          "DESCRIPTION = #{description,jdbcType=VARCHAR},",
          "ICON = #{icon,jdbcType=VARCHAR}",
        "where ID = #{id,jdbcType=VARCHAR}"
    })
    int updateByPrimaryKey(Resource record);


    @Select("select ID, NAME, PID, RESOURCETYPE, ORDERS, SCRIPTURLS, DESCRIPTION, ICON from aitk_resource_info " +
            "where  id  in (select resId from aitk_role_resources_rel where roleId = #{roleId})")
    @Results({
            @Result(column="ID", property="id", jdbcType=JdbcType.VARCHAR, id=true),
            @Result(column="NAME", property="name", jdbcType=JdbcType.VARCHAR),
            @Result(column="PID", property="pid", jdbcType=JdbcType.VARCHAR),
            @Result(column="RESOURCETYPE", property="resourceType", jdbcType=JdbcType.VARCHAR),
            @Result(column="ORDERS", property="orders", jdbcType=JdbcType.DECIMAL),
            @Result(column="SCRIPTURLS", property="scriptUrls", jdbcType=JdbcType.VARCHAR),
            @Result(column="DESCRIPTION", property="description", jdbcType=JdbcType.VARCHAR),
            @Result(column="ICON", property="icon", jdbcType=JdbcType.VARCHAR)
    })
    List<Resource> getListByRoleId(@Param("roleId") String roleId);
}
